package com.ruoyi.eqmonitor.dispose.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.eqmonitor.domain.OptDeviceAlarm;
import com.ruoyi.eqmonitor.dtos.OptDeviceAlarmDto;
import com.ruoyi.eqmonitor.dtos.OptDeviceDto;
import com.ruoyi.eqmonitor.service.IOptDeviceAlarmService;
import com.ruoyi.eqmonitor.service.IOptDeviceService;
import com.ruoyi.eqmonitor.utils.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 入流入渗报警
 */
@Slf4j
@Component
@Profile("prod")
public class InflowAndInfiltrationAlarmTask {

    @Resource
    private IOptDeviceService iOptDeviceService;

    @Resource
    private IOptDeviceAlarmService iOptDeviceAlarmService;

    /**
     * 每4小时15分执行一次 判断入流入渗
     */
    //@Scheduled(cron = "0 15 0/4 * * ?")
    /*public void validateInflowAndInfiltration() {
        // 获取今天日期
        Date now = new Date();
        // 先找到泵站直属的COD+氨氮设备
        List<OptDeviceDto> deviceList = iOptDeviceService.selectPumpStationDeviceList(ListUtil.of(Constant.EquipmentType.SR500_L_Y0_SNY.getCode()));
        if (CollectionUtil.isEmpty(deviceList)) return;
        LambdaQueryWrapper<OptDeviceAlarm> wrapper = Wrappers.lambdaQuery();
        wrapper.in(OptDeviceAlarm::getDeviceCode, deviceList.stream().map(OptDeviceDto::getDeviceCode).collect(Collectors.toSet()));
        // COD 氨氮 的 越界
        wrapper.in(OptDeviceAlarm::getAlarmPhenomenon,
                ListUtil.of(
                        Constant.AlarmPhenomenon.COD_OVERSTEP.getValue(),
                        Constant.AlarmPhenomenon.AMMONIA_NITROGEN_OVERSTEP.getValue()
                )
        );
        // 不是清除的报警
        wrapper.ne(OptDeviceAlarm::getConfirmAlarm, Constant.ConfirmAlarmType.CLEARED.getValue());
        // 现在还在持续报警
        wrapper.ne(OptDeviceAlarm::getPersistentStatus, Constant.TRUE);
        // 当天
        wrapper.between(OptDeviceAlarm::getLastAlarmTime, DateUtil.beginOfDay(now), DateUtil.endOfDay(now));
        // 查到今天 泵站 所有 COD 氨氮 的 越界
        List<OptDeviceAlarm> alarmList = iOptDeviceAlarmService.list(wrapper);
        if (CollectionUtil.isEmpty(alarmList)) return;
        for (OptDeviceAlarm optDeviceAlarm : alarmList) {
            OptDeviceAlarmDto alarmInfo;

            // 小于下限 或者大于上限
            if (NumberUtil.isLessOrEqual(NumberUtil.toBigDecimal(optDeviceAlarm.getConfigFloorLimit()), NumberUtil.toBigDecimal(optDeviceAlarm.getAlarmCodeValue()))) {
                alarmInfo = iOptDeviceAlarmService.minSewageAlarmByDeviceCode(optDeviceAlarm.getDeviceCode(), optDeviceAlarm.getAlarmPhenomenon());
            } else {
                alarmInfo = iOptDeviceAlarmService.maxSewageAlarmByDeviceCode(optDeviceAlarm.getDeviceCode(), optDeviceAlarm.getAlarmPhenomenon());
            }

            // 如果污水厂服务范围内含有报警 并且极值不是这个设备的报警 则为这个设备再生成一个入流入渗报警
            if (alarmInfo != null && !StrUtil.equals(alarmInfo.getDeviceCode(), optDeviceAlarm.getDeviceCode())) {
                OptDeviceAlarm newAlarm = new OptDeviceAlarm();
                newAlarm.setDeviceName(alarmInfo.getDeviceName());
                newAlarm.setDeviceCode(alarmInfo.getDeviceCode());
                newAlarm.setSiteName(alarmInfo.getSiteName());
                newAlarm.setDeviceType(alarmInfo.getDeviceType());
                newAlarm.setInstallTime(alarmInfo.getInstallTime());
                newAlarm.setAbx(alarmInfo.getAbx());
                newAlarm.setAby(alarmInfo.getAby());
                newAlarm.setAlarmCode(alarmInfo.getAlarmCode());
                newAlarm.setAlarmCodeValue(alarmInfo.getAlarmCodeValue());
                newAlarm.setAlarmType(alarmInfo.getAlarmType());
                // 入流入渗
                newAlarm.setAlarmPhenomenon(Constant.AlarmPhenomenon.INFLOW_INFILTRATION.getValue());
                newAlarm.setConfirmAlarm(Constant.ConfirmAlarmType.UNCONFIRMED.getValue());
                newAlarm.setAlarmCount(1);
                newAlarm.setAlarmTime(alarmInfo.getAlarmTime());
                newAlarm.setLastAlarmTime(alarmInfo.getLastAlarmTime());
                newAlarm.setConfigAlarmValue(alarmInfo.getConfigAlarmValue());
                newAlarm.setConfigFloorLimit(alarmInfo.getConfigFloorLimit());
                newAlarm.setConfigUpperLimit(alarmInfo.getConfigUpperLimit());
                newAlarm.setPersistentStatus(Constant.TRUE);
                log.info("设备: {}, 污水厂设备: {}, 发生入流入渗报警, 监测指标: {}, 上限: {}, 下限: {}, 污水厂设备数据: {}, 报警设备数据: {}",
                        alarmInfo.getDeviceCode(), optDeviceAlarm.getDeviceCode(),
                        optDeviceAlarm.getAlarmCode(), optDeviceAlarm.getConfigUpperLimit(),
                        optDeviceAlarm.getConfigFloorLimit(), optDeviceAlarm.getAlarmCodeValue(),
                        alarmInfo.getAlarmCodeValue());
                // 生成新的报警
                iOptDeviceAlarmService.doSaveAlarm(newAlarm);
            } else {
                log.info("污水厂设备: {}, 发生 {} 报警, 未找到含有相同报警的设备", optDeviceAlarm.getDeviceCode(), optDeviceAlarm.getAlarmCode());
            }
        }
    }*/
}
